//
//  GWPictureView.m
//  百思不得姐
//
//  Created by qianfeng on 16/3/4.
//  Copyright © 2016年 高炜. All rights reserved.
//

#import "GWPictureView.h"
#import "GWTopicModel.h"
#import <UIImageView+WebCache.h>
#import "GWMyCircularProgressView.h"
#import "GWTopicPictureView.h"
@interface GWPictureView ()
@property (weak, nonatomic) IBOutlet UIImageView *gifView;
@property (weak, nonatomic) IBOutlet UIImageView *pictureView;
@property (weak, nonatomic) IBOutlet UIButton *seeBigButton;
@property (weak, nonatomic) IBOutlet GWMyCircularProgressView *progress;

@end
@implementation GWPictureView
+(instancetype)pictureView
{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil]lastObject];
}
-(void)awakeFromNib
{
    
    self.autoresizingMask =UIViewAutoresizingNone;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushPictureView)]];
}
-(void)pushPictureView
{
    GWTopicPictureView *picView= [[GWTopicPictureView alloc]init];
   
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:picView animated:YES completion:nil];
     picView.topic =self.topic;
    
}
-(void)setTopic:(GWTopicModel *)topic
{
    _topic =topic;
    
    [self.pictureView sd_setImageWithURL:[NSURL URLWithString:topic.largeImage] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        self.progress.hidden =NO;
        self.topic.progress =1.0 *receivedSize/expectedSize ;
     
        [self.progress setProgress:self.topic.progress animated:NO];
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.progress.hidden =YES;
        
        if(!topic.isLongPicture)return ;
        UIGraphicsBeginImageContextWithOptions(topic.pictureF.size, YES, 0.0);
        
        CGFloat picH =topic.pictureF.size.width/topic.width *topic.height;
        [image drawInRect:CGRectMake(0, 0,topic.pictureF.size.width , picH)];
        UIImage *currentImage =  UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        self.pictureView.image =currentImage;
    }];
    
    self.gifView.hidden = ![topic.largeImage.pathExtension.lowercaseString isEqualToString:@"gif"];
    if (topic.isLongPicture) {
        self.seeBigButton.hidden =NO;
        self.pictureView.contentMode =UIViewContentModeScaleAspectFill;
    }
    else
    {
        self.seeBigButton.hidden =YES;
        self.pictureView.contentMode =UIViewContentModeScaleToFill;
    }
}
@end
